<template>
  <div class="add-card">
    <header class="add-header">
      <el-page-header content="增加月卡" @back="$router.back()" />
    </header>
    <main class="add-main">
      <div class="form-container">
        <div class="title">车辆信息</div>
        <div class="form">
          <el-form ref="form" label-width="100px" :model="form" :rules="rules">
            <el-form-item label="车主姓名" prop="personName">
              <el-input v-model="form.personName" />
            </el-form-item>
            <el-form-item label="联系方式" prop="phoneNumber">
              <el-input v-model="form.phoneNumber" />
            </el-form-item>
            <el-form-item label="车辆号码" prop="carNumber">
              <el-input v-model="form.carNumber" />
            </el-form-item>
            <el-form-item label="车辆品牌" prop="carBrand">
              <el-input v-model="form.carBrand" />
            </el-form-item>
          </el-form>
        </div>
      </div>
      <div class="form-container">
        <div class="title">最新一次月卡缴费信息</div>
        <div class="form">
          <el-form ref="feeForm" label-width="100px" :model="feeForm" :rules="feeFormRules">
            <el-form-item label="有效日期" prop="cardStartDate">
              <!-- <el-input v-model="form.cardStartDate" /> -->
              <el-date-picker
                v-model="feeForm.cardStartDate"
                type="daterange"
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
              />
            </el-form-item>
            <el-form-item label="支付金额" prop="paymentAmount">
              <el-input v-model="feeForm.paymentAmount" />
            </el-form-item>
            <el-form-item label="支付方式" prop="paymentMethod">
              <el-select v-model="feeForm.paymentMethod">
                <el-option
                  v-for="item in payMethodList"
                  :key="item.id"
                  :value="item.id"
                  :label="item.name"
                />
              </el-select>
            </el-form-item>
          </el-form>
        </div>
      </div>

    </main>
    <footer class="add-footer">
      <div class="btn-container">
        <el-button @click="reset">重置</el-button>
        <el-button type="primary" @click="add">确定</el-button>
      </div>
    </footer>
  </div>
</template>

<script>
// import { from } from 'core-js/core/array'
import { addCardAPI, getCardDetailAPI, editCardAPI } from '@/api/CarCard'
export default {
  name: 'AddCard',
  data() {
    // const validatePhoneNumber = (rule, value, callback) => {
    //   const plateNumber = /^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领][A-HJ-NP-Z][A-HJ-NP-Z0-9]{4,5}[A-HJ-NP-Z0-9挂学警港澳]$/
    //   if (plateNumber.test(value)) {
    //     callback()
    //   } else {
    //     callback(new Error('请输入正确的车牌号码'))
    //   }
    // }
    return {
      form: {
        // 车主姓名
        personName: '',
        // 联系方式
        phoneNumber: '',
        // 车牌号码
        carNumber: '',
        // 车辆品牌
        carBrand: ''
        // // 有效时间开始
        // cardStartDate: '',
        // // 有效时间结束
        // cardEndDate: '',
        // // 支付金额
        // paymentAmount: '',
        // // 支付方式
        // paymentMethod: '',
        // 企业联系人
        // enterpriseContact: ''

      },
      rules: {
        personName: [
          { required: true, message: '请输入车主姓名', trigger: 'blur' }
        ],
        // 联系方式
        phoneNumber: [
          { required: true, message: '请输入联系方式', trigger: 'blur' }
        ],
        carNumber: [
          { required: true, message: '请输入车牌号码', trigger: 'blur' },
          {
            validator: (rule, value, callback) => {
              const plateNumber = /^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领核][A-HJ-NP-Z][A-HJ-NP-Z0-9]{4,5}[A-HJ-NP-Z0-9挂学警港澳弹]$/
              if (plateNumber.test(value)) {
                callback()
              } else {
                callback(new Error('请输入正确的车牌号码'))
              }
            }
          }

        ],
        carBrand: [
          { required: true, message: '请输入车辆品牌', trigger: 'blur' }
        ]
      },
      feeFormRules: {
        cardStartDate: [
          {
            required: true,
            message: '请选择支付时间'
          }
        ],
        paymentAmount: [
          {
            required: true,
            message: '请输入支付金额',
            trigger: 'blur'
          }
        ],

        paymentMethod: [
          {
            required: true,
            message: '请选择支付方式',
            trigger: 'change'
          }
        ]
      },
      // 支付方式列表
      payMethodList: [
        {
          id: 'Alipay',
          name: '支付宝'
        },
        {
          id: 'WeChat',
          name: '微信'
        },
        {
          id: 'Cash',
          name: '线下'
        }
      ],
      feeForm: {
        // 有效时间开始
        cardStartDate: '',
        // 有效时间结束
        // cardEndDate: '',
        // 支付金额
        paymentAmount: null,
        // 支付方式
        paymentMethod: ''
      },
      feeRules: {
        cardStartDate: [
          { required: true, message: '请选择有效时间' }
        ]

      },
      list: ''
    }
  },
  mounted() {
    this.getCardDetailAPI()
  },
  methods: {
    // 重置
    reset() {
      this.$refs.form.resetFields()
    },

    async add() {
      // const payload = {
      //   ...this.form,
      //   ...this.feeForm,
      //   cardStartDate: this.feeForm.cardStartDate[0],
      //   cardEndDate: this.feeForm.cardStartDate[1]
      // }
      // // console.log(payload)

      // await addCardAPI(payload)

      // this.$refs.form.validate()
      // this.$refs.feeForm.validate()
      // this.$message.success('添加成功')
      // this.$router.push('/CarCard')
      this.$refs.form.validate(valid => {
        if (valid) {
          this.$refs.feeForm.validate(async valid => {
            if (valid) {
              const payload = {
                ...this.form,
                ...this.feeForm,
                // ...this.route.params,
                cardStartDate: this.feeForm.cardStartDate[0],
                cardEndDate: this.feeForm.cardStartDate[1]
              }
              delete payload.payTime
              const { id } = this.$route.params
              // 是否携带ID如果携带则执行修改如果没有执行添加
              // console.log('我是编辑id', id)

              if (id) {
                await editCardAPI({ ...payload })
                this.$message.success('修改成功')
                this.$router.back()
              } else {
                console.log('我是添加id')
                await addCardAPI(payload)
                this.$message.success('添加成功')
                this.$router.back()
              }
            }
          })
        }
      })
    },
    // 获取编辑详情
    async getCardDetailAPI() {
      if (!this.$route.params.id) {
        return
      }
      const res = await getCardDetailAPI(this.$route.params.id)
      console.log('我是编辑id25', res)
      this.list = res.data
      // 结构
      const { personName, phoneNumber, carNumber, carBrand, carInfoId } = res.data
      this.form = {
        personName,
        phoneNumber,
        carNumber,
        carBrand,
        carInfoId
      }
      const { rechargeId, cardStartDate, cardEndDate, paymentAmount, paymentMethod } = res.data
      this.feeForm = {
        rechargeId,
        paymentMethod,
        paymentAmount,
        cardStartDate: [cardStartDate, cardEndDate]
      }
      // 提示
      this.$nextTick(() => {
        this.$message.success('当前界面是查看信息')
      })
      // this.form = data
    }
  }
}
</script>

  <style scoped lang="scss">
  .add-card {
    background-color: #f4f6f8;
    height: 100vh;

    .add-header {
      display: flex;
      align-items: center;
      padding: 0 20px;
      height: 64px;
      background-color: #fff;

      .left {
        span {
          margin-right: 4px;
        }
        .arrow{
          cursor: pointer;
        }
      }

      .right {
        text-align: right;
      }
    }

    .add-main {
      background: #f4f6f8;
      padding: 20px 130px;

      .form-container {
        background-color: #fff;

        .title {
          height: 60px;
          line-height: 60px;
          padding-left: 20px;
        }

        .form {
          margin-bottom: 20px;
          padding: 20px 65px 24px;

          .el-form {
            display: flex;
            flex-wrap: wrap;

            .el-form-item {
              width: 50%;
            }
          }
        }
      }
      .preview{
        img{
          width: 100px;
        }
      }
    }

    .add-footer {
      position: fixed;
      bottom: 0;
      width: 100%;
      padding: 24px 50px;
      color: #000000d9;
      font-size: 14px;
      background: #fff;
      text-align: center;
    }
  }
  </style>
